﻿@inject AdsComService AdsComService
@using Microsoft.Win32
@using OneOf.Types
@using TwincatDashboard.Constants
@using TwincatDashboard.Models
@using TwincatDashboard.Services

<div class="container">
  <div class="Folder">
    <MTextField Outlined Dense
                Label="FolderFullName"
                HideDetails="@("auto")"
                @bind-Value="FolderName"
                AppendIcon="mdi-folder"
                OnAppendClick="SelectFolder"/>
  </div>
  <div class="File">
    <MTextField Outlined Dense
                Label="QuickLogFileFullName"
                HideDetails="@("auto")"
                @bind-Value="FileName"
                AppendIcon="mdi-file"/>
  </div>
  <div class="QuickPeriod">
    <MTextField Outlined Dense
                Label="QuickLogPeriod"
                Disabled
                Type="number" Suffix="ms"
                HideDetails="@("auto")"
                @bind-Value="QuickLogPeriod"/>
  </div>
  <div class="SlowPeriod">
    <MTextField Outlined Dense
                Label="SlowLogPeriod"
                Type="number" Suffix="ms"
                NumberProps="@(prop =>
                             {
                               prop.Min = SlowLogPeriodMin;
                               prop.Max = SlowLogPeriodMax;
                               prop.Step = 500;
                             })"
                Rules="[_numberRule]"
                HideDetails="@("auto")"
                @bind-Value="SlowLogPeriod"/>
  </div>
  <div class="LogType">
    <MSelect @bind-Value="ExportLogTypes"
             Items="AppConstants.SupportedLogFileTypes"
             Chips Multiple Dense
             ItemText="u => u"
             ItemValue="u => u"
             Label="Chips"/>
  </div>
  <div class="SaveConfig">
    <div style="display: flex; height: 80%; flex-direction: row;gap: 12px;  align-items: center">
      <MButton Color="primary" Outlined Dense
               Style="flex-grow: 1"
               OnClick="SaveLogConfig">
        Load Log
      </MButton>
      <MButton Color="primary" Outlined Dense
               Style="flex-grow: 1"
               OnClick="SaveLogConfig">
        Save Log
      </MButton>
    </div>
  </div>
</div>

@code {
  public string FolderName { get; set; } = string.Empty;
  public string FileName { get; set; } = string.Empty;
  public int QuickLogPeriod { get; set; } = 2;
  public int SlowLogPeriod { get; set; } = 5000;
  public List<string> ExportLogTypes = ["mat"];

  private const int SlowLogPeriodMin = 2000;
  private const int SlowLogPeriodMax = 10000;

  private Func<int, StringBoolean> _numberRule = value => value is >= SlowLogPeriodMin and <= SlowLogPeriodMax  ? true : "Must between 2000 and 10000";

  private LogConfig LogConfig { get; set; } = AppConfigService.AppConfig.LogConfig;


  protected override async Task OnInitializedAsync()
  {
    FolderName = LogConfig.FolderName;
    FileName = LogConfig.FileName;
    SlowLogPeriod = LogConfig.SlowLogPeriod;
    ExportLogTypes = LogConfig.FileType;

    QuickLogPeriod = await AdsComService.GetTaskCycleTimeAsync();
    LogConfig.QuickLogPeriod = QuickLogPeriod;
  }

  private void SaveLogConfig()
  {
    LogConfig.FolderName = FolderName;
    LogConfig.FileName = FileName;
    LogConfig.QuickLogPeriod = QuickLogPeriod;
    LogConfig.SlowLogPeriod = SlowLogPeriod;
    LogConfig.FileType = ExportLogTypes;
    AppConfigService.SaveConfig(AppConfig.ConfigFileFullName);
    Log.Information("LogConfig saved.");
  }

  private void SelectFolder(MouseEventArgs obj)
  {
    var folderDialog = new OpenFolderDialog
    {
      Title = "Select Folder",
      InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    };

    if (folderDialog.ShowDialog() != true) return;
    FolderName = folderDialog.FolderName;
    LogConfig.FolderName = FolderName;
    Log.Information("SelectFolder");
  }

}